<?php

require_once ROOT_DIR.'models/Todo.php';

/**
 * Tries to link a todo with a user and echos the resulting view
 * @param int $todoID
 * @param int $userID
 * @param string $role
 */
function setUserTodoRole($todoID, $userID, $role){
	$todo = Todo::getTodo($todoID);
	if(!is_null($todo)){
		if($todo->getCreatorID() == $_SESSION['USER_ID'] || $_SESSION['USER_NAME'] == 'admin' || RightHelper::checkIfResponsible($todo)){
			$res = Todo::linkTodoWithUser($todoID, $userID, $role);
			if($res){
				$res = new ResultJSON(false, session_id());
				echo $res->renderView();
			} else {
				$res = new ResultJSON(true, session_id(), ERROR_ACTION_FAILED);
				echo $res->renderView();
			}
		} else {
			$res = new ResultJSON(true, session_id(), ERROR_NOT_ALLOWED);
			echo $res->renderView();
		}	
	} else {
		$res = new ResultJSON(true, session_id(), ERROR_TODO_DOES_NOT_EXIST);
		echo $res->renderView();
	}
}

?>